<template>
  <div class="crypto-tool">
    <Breadcrumb />
    
    <div class="tool-header">
      <div class="container">
        <h1>加解密工具</h1>
        <div class="subtitle-group">
          <p class="subtitle">常用加解密算法工具集合</p>
        </div>
      </div>
    </div>

    <div class="tool-content">
      <div class="container">
        <div class="conversion-panel">
          <!-- 算法选择 -->
          <div class="algorithm-select">
            <h3>选择算法</h3>
            <div class="algorithm-tabs">
              <button 
                v-for="algo in algorithms" 
                :key="algo.type"
                :class="['tab-btn', { active: currentAlgo === algo.type }]"
                @click="currentAlgo = algo.type"
              >
                {{ algo.name }}
              </button>
            </div>
          </div>

          <!-- 输入区域 -->
          <div class="input-section">
            <h3>输入内容</h3>
            <div class="input-area">
              <textarea 
                v-model="inputText"
                class="custom-input"
                placeholder="请输入要处理的内容..."
                rows="5"
              ></textarea>
              <div v-if="needsKey" class="key-input">
                <input 
                  v-model="key"
                  class="custom-input"
                  :type="showKey ? 'text' : 'password'"
                  placeholder="请输入密钥"
                >
                <button class="action-btn small" @click="showKey = !showKey">
                  <i :class="showKey ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                </button>
              </div>
            </div>
          </div>

          <!-- 操作按钮 -->
          <div class="action-buttons">
            <button class="action-btn" @click="encrypt">
              <i class="fas fa-lock"></i>
              加密
            </button>
            <button class="action-btn" @click="decrypt">
              <i class="fas fa-unlock"></i>
              解密
            </button>
            <button class="action-btn" @click="clearAll">
              <i class="fas fa-trash"></i>
              清空
            </button>
          </div>

          <!-- 输出结果 -->
          <div v-if="outputText" class="output-section">
            <h3>
              处理结果
              <button 
                class="action-btn small" 
                @click="copyOutput"
                :disabled="copySuccess"
              >
                {{ copySuccess ? '复制成功' : '复制结果' }}
              </button>
            </h3>
            <div class="output-area">
              <pre><code>{{ outputText }}</code></pre>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import Breadcrumb from '@/components/Breadcrumb.vue'
import CryptoJS from 'crypto-js'

// 支持的算法
const algorithms = [
  { type: 'md5', name: 'MD5', needsKey: false },
  { type: 'sha1', name: 'SHA1', needsKey: false },
  { type: 'sha256', name: 'SHA256', needsKey: false },
  { type: 'aes', name: 'AES', needsKey: true },
  { type: 'des', name: 'DES', needsKey: true },
  { type: 'base64', name: 'Base64', needsKey: false },
  { type: 'url', name: 'URL编码', needsKey: false }
]

const currentAlgo = ref('md5')
const inputText = ref('')
const outputText = ref('')
const key = ref('')
const showKey = ref(false)
const copySuccess = ref(false)

// 是否需要密钥
const needsKey = computed(() => {
  const algo = algorithms.find(a => a.type === currentAlgo.value)
  return algo?.needsKey || false
})

// 加密
const encrypt = () => {
  if (!inputText.value) {
    alert('请输入要处理的内容')
    return
  }

  if (needsKey.value && !key.value) {
    alert('请输入密钥')
    return
  }

  try {
    switch (currentAlgo.value) {
      case 'md5':
        outputText.value = CryptoJS.MD5(inputText.value).toString()
        break
      case 'sha1':
        outputText.value = CryptoJS.SHA1(inputText.value).toString()
        break
      case 'sha256':
        outputText.value = CryptoJS.SHA256(inputText.value).toString()
        break
      case 'aes':
        outputText.value = CryptoJS.AES.encrypt(inputText.value, key.value).toString()
        break
      case 'des':
        outputText.value = CryptoJS.DES.encrypt(inputText.value, key.value).toString()
        break
      case 'base64':
        outputText.value = btoa(inputText.value)
        break
      case 'url':
        outputText.value = encodeURIComponent(inputText.value)
        break
    }
  } catch (error: any) {
    alert('处理失败: ' + error.message)
  }
}

// 解密
const decrypt = () => {
  if (!inputText.value) {
    alert('请输入要处理的内容')
    return
  }

  if (needsKey.value && !key.value) {
    alert('请输入密钥')
    return
  }

  try {
    switch (currentAlgo.value) {
      case 'aes':
        const aesBytes = CryptoJS.AES.decrypt(inputText.value, key.value)
        outputText.value = aesBytes.toString(CryptoJS.enc.Utf8)
        break
      case 'des':
        const desBytes = CryptoJS.DES.decrypt(inputText.value, key.value)
        outputText.value = desBytes.toString(CryptoJS.enc.Utf8)
        break
      case 'base64':
        outputText.value = atob(inputText.value)
        break
      case 'url':
        outputText.value = decodeURIComponent(inputText.value)
        break
      default:
        alert('该算法不支持解密')
    }
  } catch (error: any) {
    alert('处理失败: ' + error.message)
  }
}

// 复制结果
const copyOutput = async () => {
  try {
    await navigator.clipboard.writeText(outputText.value)
    copySuccess.value = true
    setTimeout(() => {
      copySuccess.value = false
    }, 3000)
  } catch (err: any) {
    alert('复制失败: ' + (err.message || '未知错误'))
  }
}

// 清空所有
const clearAll = () => {
  inputText.value = ''
  outputText.value = ''
  key.value = ''
}
</script>

<style scoped lang="scss">
.crypto-tool {
  .tool-header {
    background: linear-gradient(135deg, var(--primary-color), var(--hover-color));
    padding: 20px 0;
    color: white;
    text-align: center;
    margin-top: -1px;

    h1 {
      font-size: 1.8rem;
      margin-bottom: 0.5rem;
    }

    .subtitle-group {
      display: flex;
      flex-direction: column;
      gap: 8px;
      align-items: center;

      .subtitle {
        font-size: 0.95rem;
        opacity: 0.9;
        background: rgba(255, 255, 255, 0.1);
        padding: 6px 16px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
      }
    }
  }

  .tool-content {
    padding: 40px 0;

    .conversion-panel {
      background: white;
      border-radius: 12px;
      box-shadow: 0 5px 15px rgba(0,0,0,0.1);
      padding: 30px;
      display: flex;
      flex-direction: column;
      gap: 40px;

      h3 {
        margin-bottom: 15px;
        color: var(--text-color);
      }

      .algorithm-tabs {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        background: #f8f9fa;
        padding: 10px;
        border-radius: 8px;

        .tab-btn {
          padding: 8px 16px;
          border: none;
          background: transparent;
          color: #666;
          border-radius: 6px;
          cursor: pointer;
          transition: all 0.2s ease;

          &.active {
            background: white;
            color: var(--primary-color);
            font-weight: 500;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
          }

          &:hover:not(.active) {
            background: rgba(255,255,255,0.5);
          }
        }
      }

      .input-area, .output-area {
        background: #f8f9fa;
        padding: 20px;
        border-radius: 8px;

        textarea {
          width: 100%;
          resize: vertical;
        }

        .key-input {
          display: flex;
          gap: 10px;
          margin-top: 10px;
        }
      }

      .action-buttons {
        display: flex;
        gap: 15px;
        justify-content: center;
        margin: 20px 0;

        .action-btn {
          min-width: 100px;
          padding: 10px 20px;
          border: none;
          background: var(--primary-color);
          color: white;
          border-radius: 6px;
          font-weight: 500;
          cursor: pointer;
          transition: all 0.2s ease;
          box-shadow: 0 2px 4px rgba(0,0,0,0.1);
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 8px;

          i {
            font-size: 0.9rem;
          }

          &:hover {
            background: var(--hover-color);
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            transform: translateY(-2px);
          }

          &:active {
            transform: translateY(0);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
          }

          &:disabled {
            background: #4CAF50;
            cursor: default;
            transform: none;

            &:hover {
              background: #4CAF50;
              transform: none;
              box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
          }

          &.small {
            padding: 6px 12px;
            min-width: auto;
            font-size: 0.9rem;
            margin-left: 10px;
          }
        }
      }

      .output-area {
        pre {
          margin: 0;
          white-space: pre-wrap;
          word-break: break-all;
        }
      }
    }
  }
}
</style> 